﻿using Eneta.Portal.Forum.Core.Domain.Collections;
using Eneta.Portal.Forum.Core.Service.Application;

namespace Eneta.Portal.Forum.Core.Domain.Entities
{
    public class Category : DomainObject
    {
        // Fields
        private ForumCollection forums;

        // Methods
        private Category()
        {
        }

        public Category(string name)
        {
            Name = name;
        }

        public Category(int id, string name)
        {
            Id = id;
            Name = name;
        }

        public bool HasAccess(ForumUser user, Permission.Rights right)
        {
            foreach (Forum forum in Forums)
            {
                if (forum.HasAccess(user, right))
                {
                    return true;
                }
            }
            return false;
        }

        // Properties
        public ForumCollection Forums
        {
            get
            {
                if (forums == null)
                {
                    forums = RepositoryRegistry.ForumRepository.FindByCategoryId(base.Id);
                }
                return forums;
            }
        }

        public int SortOrder { get; set; }
    }
}
